package com.piaoniu.pndao.utils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * StringHelper
 *
 * @author T_T
 * @since 03/12/2016 20:53
 */
public class StringHelper {
    private static final char SEPARATOR = '_';


    public static boolean isEmpty(String string) {
        return string == null || "".equals(string.trim());
    }

    public static String toUnderlineName(String s) {
        if (s == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder();
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            boolean nextUpperCase = true;

            if (i < (s.length() - 1)) {
                nextUpperCase = Character.isUpperCase(s.charAt(i + 1));
            }

            if ((i >= 0) && Character.isUpperCase(c)) {
                if (!upperCase || !nextUpperCase) {
                    if (i > 0) sb.append(SEPARATOR);
                }
                upperCase = true;
            } else {
                upperCase = false;
            }

            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    public static String toCamelCase(String s) {
        if (s == null) {
            return null;
        }

        s = s.toLowerCase();

        StringBuilder sb = new StringBuilder(s.length());
        boolean upperCase = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);

            if (c == SEPARATOR) {
                upperCase = true;
            } else if (upperCase) {
                sb.append(Character.toUpperCase(c));
                upperCase = false;
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static String toCapitalizeCamelCase(String s) {
        if (s == null) {
            return null;
        }
        s = toCamelCase(s);
        return toCapitalize(s);
    }


    public static String toCapitalize(String s){
        return s.substring(0, 1).toUpperCase() + s.substring(1);
    }

    public static String join(String separator, Iterator<?> parts) {
        final StringBuilder appendable = new StringBuilder();
        if (parts.hasNext()) {
            appendable.append(parts.next());
            while (parts.hasNext()) {
                appendable.append(separator);
                appendable.append(parts.next());
            }
        }
        return appendable.toString();
    }

    public static String joinWithComma(Iterator<?> parts) {
        return join(", ", parts);
    }

    public static List<String> split(String left, String separator) {
        List<String> results = new ArrayList<>();
        if (left == null || left.isEmpty()) return results;
        for (String str : left.split(separator)) {
            if (str != null && !str.isEmpty())
                results.add(str);
        }
        return results;
    }

    public static String toColumnByStyle(String field, String style) {

        if("underline".equalsIgnoreCase(style)){
            return toUnderlineName(field);
        }

        if("camel".equalsIgnoreCase(style)){
            return toCamelCase(field);
        }

        return field;
    }

    public static String lowerFirst(String word) {
        return Character.toLowerCase(word.charAt(0)) + word.substring(1);
    }



    public static void main(String[] args) {
        System.out.println(toColumnByStyle("User_Name", "camel"));
    }
//        public static void main(String[] args) {
//            System.out.println(toUnderlineName("ISOCertifiedStaff"));
//            System.out.println(toUnderlineName("CertifiedStaff"));
//            System.out.println(toUnderlineName("UserID"));
//            System.out.println(toCamelCase("iso_certified_staff"));
//            System.out.println(toCamelCase("certified_staff"));
//            System.out.println(toCamelCase("user_id"));
//        }

}
